package view.userinteface;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import view.controller.ControllerPrincipal;
import beans.BeanProveedor;


public class JInternalFrameBajaProveedor extends javax.swing.JInternalFrame {
	private static final long serialVersionUID = 1L;
	private JLabel lblCUIT;
	private JTextField txtCUIT;
	private JButton btnAceptar;
	private JButton btnCancelar;
	private static JInternalFrameBajaProveedor instancia;
	
	public static JInternalFrameBajaProveedor getInstancia(){
		if(instancia == null)
			instancia = new JInternalFrameBajaProveedor();
		return instancia;
	}
		
	public JInternalFrameBajaProveedor() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(400, 300));
			this.setBounds(0, 0, 400, 300);
			setVisible(true);
			getContentPane().setLayout(null);
			this.setTitle("Baja de proveedor");
			this.setSize(400, 150);
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT: ");
				lblCUIT.setBounds(58, 15, 98, 23);
			}
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(174, 15, 174, 23);
			}
			{
				btnAceptar = new JButton();
				getContentPane().add(btnAceptar, "Center");
				btnAceptar.setText("Aceptar");
				btnAceptar.setBounds(58, 60, 117, 25);
				btnAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if(!txtCUIT.getText().equalsIgnoreCase("")){
							BeanProveedor beanProveedor = ControllerPrincipal.getInstancia().buscarProveedor(txtCUIT.getText());
							if(beanProveedor.getCuit()!=null && !"".equalsIgnoreCase(beanProveedor.getCuit())){
								if(ControllerPrincipal.getInstancia().borrarProveedor(beanProveedor)){
									JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"Proveedor borrado correctamente");
									dispose();
								}else{
									JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"No se pudo borrar el proveedor");
								}
							}else{
								JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"Proveedor Inexistente");
							}
						}else{
							JOptionPane.showMessageDialog(JInternalFrameModificarProveedor.getInstancia(),"Ingrese CUIT proveedor");
						}
					}
				});
			}
			{
				btnCancelar = new JButton();
				getContentPane().add(btnCancelar);
				btnCancelar.setText("Cancelar");
				btnCancelar.setBounds(212, 59, 117, 26);
				btnCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
